package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
	"zhuandan/utils"
)

type Gongchengtypelist struct {
	ID             int64                   `json:"id" gorm:"id"`
	Name           string                  `json:"name" gorm:"name"`
	Gongchengfenbu []models.Gongchengfenbu `json:"gongchengfenbu"`
}

func Gctypes(ctx *gin.Context) {
	request := GctypesBody{}
	ctx.ShouldBind(&request)
	var users []models.Gongchengtype
	var typelists []Gongchengtypelist
	data_source.Db.Find(&users)
	var fenbus []models.Gongchengfenbu
	data_source.Db.Find(&fenbus)
	var idarr []int64
	for _, v := range users {
		idarr = append(idarr, v.ID)
	}

	var yingshes []models.Gongchengyingshe
	data_source.Db.Where("gongchengtypeid in (?)", idarr).Find(&yingshes)

	for _, u := range users {
		var fenbulist []models.Gongchengfenbu
		for _, y := range yingshes {
			if u.ID == y.Gongchengtypeid {
				for _, f := range fenbus {
					if y.Gongchengfenbuid == f.ID {
						fenbulist = append(fenbulist, f)
					}
				}
			}
		}
		typelist := Gongchengtypelist{
			ID:             u.ID,
			Name:           u.Name,
			Gongchengfenbu: fenbulist,
		}
		typelists = append(typelists, typelist)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    typelists,
		"msg":     "ok",
	})
	return
}

func Gctypeyingsheupdate(ctx *gin.Context) {
	request := GctypesBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	fenbuids := request.Fenbuids
	fenbuidarr := strings.Split(fenbuids, ",")

	data_source.Db.Where("gongchengtypeid = ?", id).Delete(&models.Gongchengyingshe{})

	var yingshes []models.Gongchengyingshe
	for _, v := range fenbuidarr {
		yingshe := models.Gongchengyingshe{
			Gongchengtypeid:  id,
			Gongchengfenbuid: utils.StrToInt64(v),
		}
		yingshes = append(yingshes, yingshe)
	}
	data_source.Db.Create(&yingshes)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type GctypesBody struct {
	Page     int    `json:"pageNo" from:"pageNo"`
	Pagesize int    `json:"pageSize" from:"pageSize"`
	Name     string `json:"name" from:"name"`
	ID       int64  `json:"id" from:"id"`
	Fenbuids string `json:"fenbuids" from:"fenbuids"`
}

func Gctypeadd(ctx *gin.Context) {
	request := GctypesBody{}
	ctx.ShouldBind(&request)
	name := request.Name

	mb := models.Gongchengtype{
		Name: name,
	}
	data_source.Db.Create(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Gctypeupdate(ctx *gin.Context) {
	request := GctypesBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name

	mb := models.Gongchengtype{
		Name: name,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Gctypedel(ctx *gin.Context) {
	request := GctypesBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Gongchengtype{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type GongchengsRBody struct {
	Data       []models.Gongcheng `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

type GongchengsBody struct {
	Page     int    `json:"pageNo" from:"pageNo"`
	Pagesize int    `json:"pageSize" from:"pageSize"`
	Title    string `json:"title" gorm:"title"`
	Status   int64  `json:"status" gorm:"status"`
	Inttime  int64  `json:"inttime" gorm:"inttime"`
	Endtime  int64  `json:"endtime" gorm:"endtime"`
	Name     string `json:"name" gorm:"name"`
}

func Gongchengs(ctx *gin.Context) {
	request := GongchengsBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	inttime := request.Inttime
	endtime := request.Endtime
	title := request.Title
	name := request.Name
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Gongcheng
	res := data_source.Db

	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if inttime > 0 {
		res = res.Where("addtime >= ?", inttime)
	}
	if endtime > 0 {
		res = res.Where("addtime < ?", endtime)
	}
	if title != "" {
		res = res.Where("title like ?", "%"+title+"%")
	}
	if name != "" {
		res = res.Where("name like ?", "%"+name+"%")
	}
	res.Model(&models.Gongcheng{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("status asc, id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := GongchengsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type GongchengBody struct {
	ID       int64  `json:"id" gorm:"id"`
	Title    string `json:"title" gorm:"title"`
	Jieduan  string `json:"jieduan" gorm:"jieduan"`
	Name     string `json:"name" gorm:"name"`
	Classify string `json:"classify" gorm:"classify"`
	Content  string `json:"content" gorm:"content"`
	Zbtime   string `json:"zbtime" gorm:"zbtime"`
	Gongqi   string `json:"gongqi" gorm:"gongqi"`
	Status   int64  `json:"status" gorm:"status"` // 1新录入 2挖掘中 3挖掘完成
}

func Gongchengsadd(ctx *gin.Context) {
	request := GongchengBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	title := request.Title
	jieduan := request.Jieduan
	classify := request.Classify
	content := request.Content
	zbtime := request.Zbtime
	gongqi := request.Gongqi
	var gc models.Gongcheng
	data_source.Db.Where("title = ?", title).First(&gc)
	if gc.ID > 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "重复数据",
		})
		return
	}
	mb := models.Gongcheng{
		Name:     name,
		Title:    title,
		Jieduan:  jieduan,
		Classify: classify,
		Content:  content,
		Zbtime:   zbtime,
		Gongqi:   gongqi,
		Status:   1,
		Addtime:  time.Now().Unix(),
	}
	data_source.Db.Create(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Gongchengsupdate(ctx *gin.Context) {
	request := GongchengBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	title := request.Title
	jieduan := request.Jieduan
	classify := request.Classify
	content := request.Content
	zbtime := request.Zbtime
	gongqi := request.Gongqi

	mb := models.Gongcheng{
		Name:     name,
		Title:    title,
		Jieduan:  jieduan,
		Classify: classify,
		Content:  content,
		Zbtime:   zbtime,
		Gongqi:   gongqi,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Gongchengstatus(ctx *gin.Context) {
	request := GongchengBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status

	mb := models.Gongcheng{
		Status: status,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Gongchengsdel(ctx *gin.Context) {
	request := GongchengBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Gongcheng{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type GongchenginfoRBody struct {
	Gongcheng       models.Gongcheng     `json:"gongcheng"`
	Gongchengfenbus []Gongchengfenbuinfo `json:"gongchengfenbus"`
}
type Gongchengfenbuinfo struct {
	ID             int64                   `json:"id" gorm:"id"`
	Name           string                  `json:"name" gorm:"name"`
	Cailiao        string                  `json:"cailiao" gorm:"cailiao"`
	Gongchengwajue []models.Gongchengwajue `json:"gongchengwajue"`
}

func Gongchenginfo(ctx *gin.Context) {
	request := GongchengBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var gongcheng models.Gongcheng
	data_source.Db.Where("id = ?", id).First(&gongcheng)

	var gongchengtype models.Gongchengtype
	data_source.Db.Where("name = ?", gongcheng.Classify).First(&gongchengtype)
	var yingshes []models.Gongchengyingshe
	data_source.Db.Where("gongchengtypeid = ?", gongchengtype.ID).Find(&yingshes)
	var fenbuidarr []int64
	for _, y := range yingshes {
		fenbuidarr = append(fenbuidarr, y.Gongchengfenbuid)
	}

	var gongchengfenbus []models.Gongchengfenbu
	data_source.Db.Where("id  in (?)", fenbuidarr).Find(&gongchengfenbus)
	var gongchengwajues []models.Gongchengwajue
	data_source.Db.Where("gongchengfenbuid in (?)", fenbuidarr).Where("gongchengid = ?", id).Find(&gongchengwajues)

	var fengbus []Gongchengfenbuinfo
	for _, f := range gongchengfenbus {
		var gongchengwajuelist []models.Gongchengwajue
		for _, w := range gongchengwajues {
			if f.ID == w.Gongchengfenbuid {
				gongchengwajuelist = append(gongchengwajuelist, w)
			}
		}
		fengbu := Gongchengfenbuinfo{
			ID:             f.ID,
			Name:           f.Name,
			Cailiao:        f.Cailiao,
			Gongchengwajue: gongchengwajuelist,
		}
		fengbus = append(fengbus, fengbu)
	}
	data := GongchenginfoRBody{
		Gongcheng:       gongcheng,
		Gongchengfenbus: fengbus,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Gongchengfenbu(ctx *gin.Context) {
	request := GongchengBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var gongcheng models.Gongcheng
	data_source.Db.Where("id = ?", id).First(&gongcheng)
	var gongchengtype models.Gongchengtype
	data_source.Db.Where("name = ?", gongcheng.Classify).First(&gongchengtype)
	var yingshes []models.Gongchengyingshe
	data_source.Db.Where("gongchengtypeid = ?", gongchengtype.ID).Find(&yingshes)
	var fenbuidarr []int64
	for _, y := range yingshes {
		fenbuidarr = append(fenbuidarr, y.Gongchengfenbuid)
	}

	var gongchengfenbus []models.Gongchengfenbu
	data_source.Db.Where("id  in (?)", fenbuidarr).Find(&gongchengfenbus)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    gongchengfenbus,
		"msg":     "ok",
	})
	return
}

type WajueBody struct {
	Gongchengfenbuid int64  `json:"gongchengfenbuid" gorm:"gongchengfenbuid"`
	Adminid          int64  `json:"adminid" gorm:"adminid"`
	Adminname        string `json:"adminname" gorm:"adminname"`
	Addtime          int64  `json:"addtime" gorm:"addtime"`
	Status           int64  `json:"status" gorm:"status"` // 1未接通  2 接通挂断 3接通
	Cailiao          string `json:"cailiao" gorm:"cailiao"`
	Content          string `json:"content" gorm:"content"`
	Gongchengid      int64  `json:"gongchengid" gorm:"gongchengid"`
}

func Wajueadd(ctx *gin.Context) {
	request := WajueBody{}
	ctx.ShouldBind(&request)
	gongchengfenbuid := request.Gongchengfenbuid
	adminid := request.Adminid
	adminname := request.Adminname
	status := request.Status
	cailiao := request.Cailiao
	content := request.Content
	gongchengid := request.Gongchengid

	mb := models.Gongchengwajue{
		Gongchengfenbuid: gongchengfenbuid,
		Addtime:          time.Now().Unix(),
		Adminid:          adminid,
		Adminname:        adminname,
		Status:           status,
		Content:          content,
		Cailiao:          cailiao,
		Gongchengid:      gongchengid,
	}
	data_source.Db.Create(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Gongchengfbs(ctx *gin.Context) {

	var users []models.Gongchengfenbu
	data_source.Db.Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

type GongchengfbBody struct {
	ID      int64  `json:"id" gorm:"id"`
	Name    string `json:"name" gorm:"name"`
	Cailiao string `json:"cailiao" gorm:"cailiao"`
}

func Gongchengfbadd(ctx *gin.Context) {
	request := GongchengfbBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	cailiao := request.Cailiao

	mb := models.Gongchengfenbu{
		Name:    name,
		Cailiao: cailiao,
	}
	data_source.Db.Create(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Gongchengfbupdate(ctx *gin.Context) {
	request := GongchengfbBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	cailiao := request.Cailiao

	mb := models.Gongchengfenbu{
		Name:    name,
		Cailiao: cailiao,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Gongchengfbdel(ctx *gin.Context) {
	request := GongchengBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Gongchengfenbu{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
